package api.net;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import okhttp3.ResponseBody;
import retrofit2.Converter;

import java.io.IOException;
//JOSN转换
public class CustomGsonResponseConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    CustomGsonResponseConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        try {
            String body = value.string();
//            if (body.contains("status\":\"400\"") || null == body || body.trim().equals("")) {
//                body = "{\"returnCode\":\"400\",\"message\":\"返回数据异常>>>>\",\"dataInfo\":null}";
//            }
            return adapter.fromJson(body);
        } catch (Exception e) {
//            return adapter.fromJson( "{\"returnCode\":\"400\",\"message\":\"返回数据异常>>>>以原格式不一样\",\"dataInfo\":null}");
//            throw new RuntimeException(e.getMessage());
            return null;
        } finally {
            value.close();
        }
    }
}
